﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLNH_DAL_WS
{
    public class OrderDetailDAO
    {
        public int GenerateOrderNo()
        {
            return (DateTime.Now.Hour * 3600 + DateTime.Now.Minute * 60 + DateTime.Now.Second);
        }

        public List<OrderDetailDTO> GetAllOrderDetail()
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            List<OrderDetailDTO> result = new List<OrderDetailDTO>();

            var orders = from order in data.ORDERs
                         select order;

            if (orders.Count() > 0)
            {
                foreach (ORDER order in orders)
                {
                    OrderDetailDTO orderDetail = new OrderDetailDTO()
                    {
                        OrderNo = order.OrderNo,
                        UserName = order.UserName,
                        TableID = order.TableID
                    };

                    if (order.State.Equals(State.Modified.ToString()))
                    {
                        orderDetail.State = State.Modified;
                    }
                    else if (order.State.Equals(State.New.ToString()))
                    {
                        orderDetail.State = State.New;
                    }
                    else if (order.State.Equals(State.ReadyToServe.ToString()))
                    {
                        orderDetail.State = State.ReadyToServe;
                    }
                    else if (order.State.Equals(State.Served.ToString()))
                    {
                        orderDetail.State = State.Served;
                    }

                    orderDetail.Meals = new List<OrderMealDTO>();

                    foreach (ORDER_MEAL meal in order.ORDER_MEALs)
                    {
                        orderDetail.Meals.Add(new OrderMealDTO()
                        {
                            OrderNo = order.OrderNo,
                            MealID = meal.MealID,
                            MealName = meal.MealName,
                            Amount = Int32.Parse(meal.Amount.ToString())
                        });
                    }

                    result.Add(orderDetail);
                }
            }
            return result;
        }

        public OrderDetailDTO GetOrder(int orderNo)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var orders = from order in data.ORDERs
                         where order.OrderNo == orderNo
                         select order;

            if (orders.Count() > 0)
            {
                OrderDetailDTO result = new OrderDetailDTO()
                {
                    OrderNo = orderNo,
                    TableID = orders.First().TableID,
                    UserName = orders.First().UserName
                };

                if (orders.First().State.Equals(State.Modified.ToString()))
                {
                    result.State = State.Modified;
                }
                else if (orders.First().State.Equals(State.New.ToString()))
                {
                    result.State = State.New;
                }
                else if (orders.First().State.Equals(State.ReadyToServe.ToString()))
                {
                    result.State = State.ReadyToServe;
                }
                else if (orders.First().State.Equals(State.Served.ToString()))
                {
                    result.State = State.Served;
                }

                result.Meals = new List<OrderMealDTO>();

                foreach (ORDER_MEAL meal in orders.First().ORDER_MEALs)
                {
                    result.Meals.Add(new OrderMealDTO()
                    {
                        OrderNo = orderNo,
                        MealID = meal.MealID,
                        MealName = meal.MealName,
                        Amount = Int32.Parse(meal.Amount.ToString())
                    });
                }

                return result;
            }

            return null;
        }

        public void AddOrder(OrderDetailDTO order)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            ORDER item = new ORDER()
                        {
                            OrderNo = order.OrderNo,
                            State = order.State.ToString(),
                            TableID = order.TableID,
                            UserName = order.UserName
                        };

            item.ORDER_MEALs = new System.Data.Linq.EntitySet<ORDER_MEAL>();

            foreach (OrderMealDTO meal in order.Meals)
            {
                item.ORDER_MEALs.Add(new ORDER_MEAL()
                {
                    OrderNo = order.OrderNo,
                    MealID = meal.MealID,
                    MealName = meal.MealName,
                    Amount = meal.Amount
                });
            }

            data.ORDERs.InsertOnSubmit(item);
            data.SubmitChanges();
        }

        public void DeleteOrder(int orderNo)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var orders = from order in data.ORDERs
                         where order.OrderNo == orderNo
                         select order;
            if (orders.Count() > 0)
            {
                ORDER order = orders.First();

                for (int i = 0; i < order.ORDER_MEALs.Count; i++)
                {
                    data.ORDER_MEALs.DeleteOnSubmit(order.ORDER_MEALs[i]);
                }

                data.ORDERs.DeleteOnSubmit(orders.First());
            }

            data.SubmitChanges();
        }

        public void UpdateOrder(OrderDetailDTO order)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var orders = from o in data.ORDERs
                         where o.OrderNo == order.OrderNo
                         select o;
            if (orders.Count() > 0)
            {
                ORDER o = orders.First();
                o.State = order.State.ToString();
                o.TableID = order.TableID;
                o.UserName = order.UserName;

                for (int i = 0; i < o.ORDER_MEALs.Count(); i++)
                {
                    data.ORDER_MEALs.DeleteOnSubmit(o.ORDER_MEALs[i]);
                }

                for (int i = 0; i < order.Meals.Count; i++)
                {
                    o.ORDER_MEALs.Add(new ORDER_MEAL()
                    {
                        OrderNo = order.OrderNo,
                        MealID = order.Meals[i].MealID,
                        MealName = order.Meals[i].MealName,
                        Amount = order.Meals[i].Amount
                    });
                }

                data.SubmitChanges();
            }
        }

        public void UpdateOrderState(int orderNo, State state)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var orders = from o in data.ORDERs
                         where o.OrderNo == orderNo
                         select o;

            if (orders.Count() > 0)
            {
                orders.First().State = state.ToString();
            }

            data.SubmitChanges();
        }
    }
}